package com.example.musicaiap.service

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.media3.ui.PlayerNotificationManager

/**
 * 媒体按钮广播接收器
 * 用于接收通知栏按钮点击的广播事件，并执行相应操作
 */
class MediaButtonReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            // 处理播放控制按钮
            PlayerNotificationManager.ACTION_PLAY -> {
                // 发送播放命令给MusicService
                val serviceIntent = Intent(context, MusicService::class.java).apply {
                    action = MusicService.ACTION_PLAY
                }
                context.startService(serviceIntent)
            }
            PlayerNotificationManager.ACTION_PAUSE -> {
                // 发送暂停命令给MusicService
                val serviceIntent = Intent(context, MusicService::class.java).apply {
                    action = MusicService.ACTION_PAUSE
                }
                context.startService(serviceIntent)
            }
            PlayerNotificationManager.ACTION_NEXT -> {
                // 发送下一首命令给MusicService
                val serviceIntent = Intent(context, MusicService::class.java).apply {
                    action = MusicService.ACTION_NEXT
                }
                context.startService(serviceIntent)
            }
            PlayerNotificationManager.ACTION_PREVIOUS -> {
                // 发送上一首命令给MusicService
                val serviceIntent = Intent(context, MusicService::class.java).apply {
                    action = MusicService.ACTION_PREVIOUS
                }
                context.startService(serviceIntent)
            }
            
            // 处理自定义按钮
            MusicService.COMMAND_TOGGLE_FAVORITE -> {
                // 显示收藏按钮点击的提示
                Toast.makeText(context, "收藏功能开发中", Toast.LENGTH_SHORT).show()
                
                // 发送收藏命令给MusicService
                val serviceIntent = Intent(context, MusicService::class.java).apply {
                    action = MusicService.COMMAND_TOGGLE_FAVORITE
                }
                context.startService(serviceIntent)
            }
            MusicService.COMMAND_SHOW_LYRICS -> {
                // 显示歌词按钮点击的提示
                Toast.makeText(context, "歌词功能开发中", Toast.LENGTH_SHORT).show()
                
                // 发送歌词命令给MusicService
                val serviceIntent = Intent(context, MusicService::class.java).apply {
                    action = MusicService.COMMAND_SHOW_LYRICS
                }
                context.startService(serviceIntent)
            }
        }
    }
} 